<template>
  <el-dialog :title="title" :visible="showDialog" @close="close">
    <el-form ref="myref" v-loading="loading" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <!-- 下拉 -->
      <el-form-item prop="managerId" label="部门负责人">
        <el-select v-model="formData.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
          <el-option v-for=" item in simpleList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="formData.introduce" placeholder="1-100个字符" type="textarea" size="mini" :rows="4" style="width: 80%" />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="handieSubmit">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { reqGteSimpl, reqaddDeP, reqGteEdit, requpdataEdit } from '@/api/department'
export default {

  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    souceDpts: {
      type: Array,
      require: true,
      default: () => []
    },
    pid: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      formData: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '' // 父级部门的id
      },
      title: '',
      loading: false,
      rules: {
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur'
          },
          { validator: async(rule, value, cb) => {
            if (this.formData.id && value === this.denpInfo.code) {
              return cb()
            }
            this.souceDpts.find(item => item.code === value) ? cb(new Error(`已存在${value}的数据`)) : cb()
          }, trigger: 'change' }
        ], // 部门编码
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }, {
          min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'
        }], // 部门介绍
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }], // 部门负责人id
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur'
          },
          { validator: async(rule, value, cb) => {
            if (this.formData.id && value === this.denpInfo.name) {
              return cb()
            }
            this.souceDpts.find(item => item.name === value) ? cb(new Error(`已存在${value}的数据`)) : cb()
          }, trigger: 'change' }] // 部门名称
      },
      simpleList: [],
      denpInfo: {}
    }
  },
  computed: {

  },
  watch: {
  },
  async created() {
    const res = await reqGteSimpl()
    this.simpleList = res.data
    // 只会重置from的内容 id什么都不会
    this.formData = {
      code: '', // 部门编码
      introduce: '', // 部门介绍
      managerId: '', // 部门负责人id
      name: '' // 部门名称

    }
  },
  mounted() {

  },
  methods: {
    close() {
      this.$emit('update:showDialog', false)
      this.$refs.myref.resetFields()
    },
    handieSubmit() {
      console.log({ ...this.formData, pid: this.pid })
      this.$refs.myref.validate(async boo => {
        if (boo) {
          if (this.formData.id) {
            console.log(1)
            // 果然是编辑 发送请求
            await requpdataEdit(this.formData)
          } else {
            await reqaddDeP({ ...this.formData, pid: this.pid })
          }

          this.close()
          this.$emit('reload')
        }
      })
    },
    // 数据回显显示编辑的内容
    async   showEdiDept() {
      // console.log('编辑内容回显')
      this.loading = true
      const res = await reqGteEdit(this.pid)
      this.formData = res.data
      // 存一份数据做对比  这个数据是死的 fromdada里面的数据是双向绑定的
      this.denpInfo = { ...res.data }
      console.log(res)
      this.loading = false
    }
    // async showEdiDept(id) {
    //   const res = await reqGteEdit(id)
    //   this.formData = res.data
    //   console.log(res.data)
    //   // console.log(this.formData.id)
    // }
  }
}
</script>

<style scoped>

</style>
